package com.quectel.cms.controller.ai;


import com.quectel.cms.controller.BaseController;
import com.quectel.cms.vo.ai.AiControlCarSaveVo;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.ai.dto.AiControlCarDto;
import com.quectel.core.module.ai.dto.AiControlLibDto;
import com.quectel.core.module.ai.service.AiControlCarService;
import com.quectel.core.module.ai.service.AiControlLibService;
import com.quectel.core.module.car.dto.CarDto;
import com.quectel.core.module.car.service.CarService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author rananxin
 * @date 2021-11-24 19:13:09
 */
@RestController
@RequestMapping("aiControlCar")
@Tag(name = "AiControlCarController", description = "布控车辆相关api")
public class AiControlCarController extends BaseController {

    @DubboReference
    private AiControlCarService aiControlCarService;
    @DubboReference
    private AiControlLibService aiControlLibService;
    @DubboReference
    private CarService carService;

    /**
     * 列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "aiControlCar:list")
    @Operation(summary = "布控车辆的分页查询列表")
    public Response<Page<AiControlCarDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = aiControlCarService.queryTotal(request.getParams());

        List<AiControlCarDto> list = null;
        if (total > 0) {
            list = aiControlCarService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<AiControlCarDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<AiControlCarDto>>ok().wrap(page);
    }


    /**
     * 信息
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "aiControlCar:info")
    @Operation(summary = "通过id查询单条布控车辆记录")
    public Response<AiControlCarDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (aiControlCarService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        AiControlCarDto aiControlCarDto = aiControlCarService.selectById(id);

        return Response.<AiControlCarDto>ok().wrap(aiControlCarDto);
    }

    /**
     * 保存
     */
    @PostMapping("save")
    @RequirePermissions(values = "aiControlCar:edit")
    @Operation(summary = "新增布控车辆记录")
    public Response<Object> save(@RequestBody @Validated AiControlCarSaveVo aiControlCarSaveVo) {

        AiControlLibDto aiControlLibDto = aiControlLibService.selectById(aiControlCarSaveVo.getLibId());
        if (aiControlLibDto == null) {
            return Response.error("lib not exist");
        }

        List<AiControlCarDto> list = Arrays.stream(aiControlCarSaveVo.getCarIds())
                .map(id -> {
                    CarDto carDto = carService.selectById(id);
                    if (carDto == null) {
                        return null;
                    }
                    AiControlCarDto dto = CopyUtils.copyObj(carDto, AiControlCarDto.class);
                    dto.setAiControlLibId(aiControlLibDto.getId());
                    fillSaveCommonData(dto);
                    return dto;
                })
                .filter(o -> o != null)
                .collect(Collectors.toList());

        aiControlCarService.saveBatch(list);
        return Response.ok();
    }

    /**
     * 修改
     */
    @PostMapping("update")
    @RequirePermissions(values = "aiControlCar:edit")
    @Operation(summary = "修改单条布控车辆记录")
    public Response<Object> update(@RequestBody @Validated AiControlCarDto aiControlCarDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(aiControlCarDto.getId()));
        if (aiControlCarService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(aiControlCarDto);

        aiControlCarService.updateById(aiControlCarDto);

        return Response.ok();
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @RequirePermissions(values = "aiControlCar:delete")
    @Operation(summary = "批量删除存在的布控车辆记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (aiControlCarService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        aiControlCarService.deleteBatch(ids);

        return Response.ok();
    }
}
